<!--
 * @Description:
 * @Author: ZachGmy
 * @Date: 2020-09-03 09:57:40
 * @LastEditors: ZachGmy
 * @LastEditTime: 2022-06-15 09:48:56
-->
<template>
  <!-- 定点医院管理 -->
  <div>
    <!-- 新增+搜索框 -->
    <div class="AddSearchInput">
      <!-- 新增按钮 -->
      <div class="addBtn">
        <el-button type="primary" round @click="toAddBtn">新增</el-button>
      </div>
      <!-- 输入框 -->
      <div class="searchInput">
        <el-input
          placeholder="请输入医院名称"
          class="input"
          v-model="hosName"
          clearable
        >
        </el-input>
        <!-- 搜素按钮 -->
        <el-button type="primary" round @click="getHosMaData">搜索</el-button>
      </div>
    </div>
    <!-- 全部数据展示 -->
    <div class="hosDataDisply">
      <el-table :data="hosMaData" style="width: 100%">
        <!-- 序号 -->
        <el-table-column type="index" label="序号" width="180">
        </el-table-column>
        <!-- 医院名称 -->
        <el-table-column prop="name" label="医院名称" width="180">
        </el-table-column>
        <!-- 医院简介 -->
        <el-table-column prop="introduce" label="医院简介"> </el-table-column>
        <!-- 医院地址-->
        <el-table-column prop="address" label="医院地址"> </el-table-column>
        <!-- 标记 -->
        <el-table-column prop="flags" label="标记"> </el-table-column>
        <!-- 经度 -->
        <el-table-column prop="longitude" label="经度"> </el-table-column>
        <!-- 维度 -->
        <el-table-column prop="latitude" label="维度"> </el-table-column>
        <!-- 操作 -->
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="toEditNews(scope.row)" type="text" size="small"
              >修改</el-button
            >
            <el-button type="text" size="small" @click="toDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 模态框 -->
    <el-dialog
      v-if="dialogVisible"
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="60%"
    >
      <!--表单区域 -->
      <el-form
        :model="hosMaForm"
        :rules="hosMaRules"
        ref="hosMaForm"
        label-width="100px"
      >
        <!-- 医院名称 -->
        <el-form-item label="名称" prop="name">
          <el-input v-model="hosMaForm.name"></el-input>
        </el-form-item>
        <!-- 介绍 -->
        <el-form-item label="介绍" prop="introduce">
          <el-input type="textarea" v-model="hosMaForm.introduce"></el-input>
        </el-form-item>
        <!-- 省市 -->
        <el-form-item label="省市" prop="form">
          <el-cascader
            v-model="form.pca"
            :props="{ label: 'name', value: 'name' }"
            :options="pca"
          ></el-cascader>
        </el-form-item>
        <!-- 详细地址 -->
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="hosMaForm.address"></el-input>
        </el-form-item>
        <!-- 标记 -->
        <el-form-item label="标记" prop="flags">
          <el-input v-model="hosMaForm.flags"></el-input>
        </el-form-item>
        <!-- 经度 -->
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="hosMaForm.longitude"></el-input>
        </el-form-item>
        <!-- 纬度 -->
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="hosMaForm.latitude"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="toSubmitHosMa">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 导入接口
import pca from "@/assets/pca.json";
import { get, post } from "@/utils/request";
export default {
  data() {
    return {
      form: {},
      pca,
      // 医院管理表单对象(新增)
      hosMaForm: {},
      //医院管理表单验证规则
      hosMaRules: {
        // 新闻标题的表单验证规则
        name: [{ required: true, message: "请输入医院名称", trigger: "blur" }],
        pca: [{ required: true, message: "请选择所属分类", trigger: "change" }],

        address: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
        longitude: [{ required: true, message: "请输入经度", trigger: "blur" }],
        latitude: [{ required: true, message: "请输入纬度", trigger: "blur" }],
      },

      // 获取全部医院信息 数组
      hosMaData: [],

      // 设置模态框的标题
      dialogTitle: "",

      dialogVisible: false,
      hosName: "",
      page: 1,
      pageSize: 30,
    };
  },
  created() {
    this.getHosMaData();
  },
  methods: {
    // 模态框确认按钮(新增\修改确认)
    toSubmitHosMa() {
      // console.log("123");
      // 表单验证提交方法
      this.$refs["hosMaForm"].validate(async (valid) => {
        if (valid) {
          console.log(1111, this.form);

          this.hosMaForm = {
            ...this.hosMaForm,
            province: this.form.pca[0],
            city: this.form.pca[1],
            area: this.form.pca[2],
          };
          console.log(22222, this.hosMaForm);
          let res = await post("/hospital/saveOrUpdate", this.hosMaForm);
          console.log(3333, res);
          if (res.status == 200) {
            // 提交成功之后的提示
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
            // 关闭模态框
            this.dialogVisible = false;
            // 刷新页面
            this.getHosMaData();
          } else {
            // 失败的提示
            this.$notify({
              title: "失败",
              message: res.message,
              type: "error",
            });
          }
        } else {
          return false;
        }
      });
    },
    // 新增按钮
    toAddBtn() {
      // 清空表单数据
      this.hosMaForm = {};
      this.dialogTitle = "新增定点医院信息";
      this.dialogVisible = true;
    },
    // 编辑
    toEditNews(row) {
      // console.log("123");
      // 确认模态框标题
      this.dialogTitle = "修改定点医院信息";
      // 获取点当条数据的值（row报错，为什么？没传参数, 什么要...row？这个row是 全部数据的row吗？）
      this.hosMaForm = { ...row };
      // 弹出模态框
      this.dialogVisible = true;
      // 刷新页面数据
      this.getHosMaData();
    },
    // 删除
    toDelete(id) {
      // console.log("123");
      // 消息提示是否删除
      this.$confirm("此操作将永久删除该数据,是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        // 点击确定按钮执行的方法
        .then(async () => {
          let res = await get("/hospital/deleteById", { id });
          if (res.status == 200) {
            this.$message({
              type: "success",
              message: res.message,
            });
            // 刷新页面
            this.getHosMaData();
          } else {
            this.$message({
              type: "error",
              message: "删除失败!",
            });
          }
        })
        // 点击取消时
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 获取全部医院信息
    async getHosMaData() {
      // 设置参数
      let params = {
        page: this.page,
        pageSize: this.pageSize,
        name: this.hosName,
      };
      // 发送请求
      let res = await get("/hospital/pageQuery", params);
      // console.log(res.data.list);
      // 赋值
      this.hosMaData = res.data.list;
    },
  },
};
</script>

<style scoped>
.AddSearchInput {
  width: 100%;
  display: flex;
}
.searchInput {
  display: flex;
  width: 33%;
}
.searchInput .input {
  margin-right: 5px;
}
.AddSearchInput .addBtn {
  flex: 1;
}
</style>
